<?php

/**
 * This is the model class for table "projects".
 *
 * The followings are the available columns in table 'projects':
 * @property string $id
 * @property string $name
 * @property string $created
 * @property string $owner_id
 */
class Projects extends JLActiveRecord
{
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Projects the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'projects';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, owner_id', 'length', 'max'=>16),
			array('name, created', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, created, owner_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'created' => 'Created',
			'owner_id' => 'Owner',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('owner_id',$this->owner_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function searchProjects($q){
	
		$criteria=new CDbCriteria(); 
		 
		$criteria->addSearchCondition('name',$q,true); 
		

		$projects = $this->findAll($criteria);
		$data = array();
		if(!empty($projects)){
			foreach($projects as $key=>$project){
				$dir = "projects/".outStr(currentUser(true)->id) ."/".$project->name;
				$f = FileTree::php_file_tree($dir,"",array(),$project->name,outStr($project->id));
				$project->id = outStr($project->id);
				$project->owner_id = outStr($project->owner_id);
				$data[$key] = $project->attributes;
				$data[$key]['dir']=$f;
			}
		}
		return $data;
	
	}	
	public function gets(){
	
		$criteria=new CDbCriteria(); 
		 
		
		

		$projects = $this->findAll($criteria);
		$data = array();
		if(!empty($projects)){
			foreach($projects as $key=>$project){
				$dir = "projects/".outStr(currentUser(true)->id) ."/".$project->name;
				$f = FileTree::php_file_tree($dir,"",array(),$project->name,outStr($project->id));
				$project->id = outStr($project->id);
				$project->owner_id = outStr($project->owner_id);
				$data[$key] = $project->attributes;
				$data[$key]['dir']=$f;
			}
		}
		return $data;
	
	}
	
	public function createTree(){
		$structure = 'projects/'.outStr(currentUser(true)->id);
		@mkdir($structure, 0700);			
		$structure = 'projects/'.outStr(currentUser(true)->id)."/".$this->name;
		@mkdir($structure, 0700);


		$structure = 'projects/source/'.outStr(currentUser(true)->id);
		@mkdir($structure, 0700);			
		$structure = 'projects/source/'.outStr(currentUser(true)->id)."/".$this->name;
		@mkdir($structure, 0700);
		
	}
}